<?php

namespace app\admin\model;

use think\Model;
use traits\model\SoftDelete;

class Schedule extends Model
{

    use SoftDelete;

    

    // 表名
    protected $name = 'schedule';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = 'deletetime';

    // 追加属性
    protected $append = [
//        'status_text'
    ];

    public function getOriginData()
    {
        return $this->origin;
    }

    protected static function init()
    {
        self::beforeUpdate(function ($row) {
            $changed = $row->getChangedData();
            $origin = $row->getOriginData();
            // //如果有修改开始时间
            // if (isset($changed['date'])) {
            //     $row->full_start_time = $changed['date'].' '.$origin['start_time'];
            // }
            // if (isset($changed['start_time'])) {
            //     $row->full_start_time = $origin['date'].' '.$changed['start_time'];
            // }
            $dateChanged = isset($changed['date']);
            $startTimeChanged = isset($changed['start_time']);

            if ($dateChanged || $startTimeChanged) {
                // 根据变化情况更新 full_start_time
                $newDate = $dateChanged ? $changed['date'] : $origin['date'];
                $newStartTime = $startTimeChanged ? $changed['start_time'] : $origin['start_time'];
                $row->full_start_time = $newDate . ' ' . $newStartTime;

                // 修正原代码中此处应更新 full_start_time 而非 full_end_time
                if ($dateChanged && $startTimeChanged) {
                    $row->full_start_time = $newDate . ' ' . $newStartTime;
                }
            }
        });

        self::beforeInsert(function ($model) {
            $model->full_start_time = $model['date'].' '.$model['start_time'];
        });

    }

    
    public function getStatusList()
    {
        return ['1' => __('Status 1'), '0' => __('Status 0')];
    }


    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    public function hall()
    {
        return $this->belongsTo('Hall', 'hall_id', 'id', [], 'LEFT')->field('id,name,image');
    }

    public function film()
    {
        return $this->belongsTo('app\admin\model\film\Info', 'film_info_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }


}
